package com.bluefish.bltuan.action.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.annotation.Before;
import com.bluefish.bltuan.cache.ApplicationUtils;
import com.bluefish.bltuan.module.cms.Content;
import com.bluefish.bltuan.module.cms.ContentType;
import com.bluefish.bltuan.service.ContentService;

public class CmsAction extends AdminAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6481117911624562464L;

	private List<Content> contents;
	
	private ContentType type;
	
	private ContentService contentService;
	
	private Content currentContent;

	private String title;

	private String nick;

	private boolean link;

	private String content;

	private String website; 
	
	@Before(include={"edit","update","destroy"})
	public void prepare(){
		if(this.getId() != null)
			this.currentContent = this.contentService.findById(getIntegerId());
	}
	
	@Override
	public HttpHeaders create() {
		this.currentContent = new Content();
		setProperties();
		this.contentService.save(this.currentContent);
		updateCache();
		redirect();
		return null;
	}

	private void updateCache() {
		ServletContext context = ServletActionContext.getServletContext();
		switch (this.type) {
		case INTRODUCTION:
			ApplicationUtils.updateIntroductions(context, contentService);
			break;
		case FRIENDLY_LINK:
			ApplicationUtils.updateFriendlinks(context, contentService);
			break;
		case QUESTION:
			ApplicationUtils.updateQuestions(context, contentService);
			break;
		default:
			break;
		}
	}

	private void setProperties() {
		this.currentContent.setTitle(title);
		this.currentContent.setNick(nick);
		this.currentContent.setType(type);
		this.currentContent.setLink(link);
		this.currentContent.setContent(content);
		this.currentContent.setWebsite(website);
		this.currentContent.setLastModified(new Date());
	}

	@Override
	public String destroy() {
		if(this.currentContent != null){
			this.contentService.delete(this.currentContent);
		}
		updateCache();
		redirect();
		return null;
	}

	@Override
	public String edit() {
		return EDIT;
	}

	@Override
	public HttpHeaders editNew() {
		return new DefaultHttpHeaders(NEW).disableCaching();
	}

	@Override
	public HttpHeaders index() {
		return null;
	}

	@Override
	public HttpHeaders show() {
		this.type = ContentType.valueOfString(this.getId());
		this.contents = this.contentService.list(this.type);
		return new DefaultHttpHeaders(SHOW).disableCaching();
	}

	@Override
	public String update() {
		setProperties();
		this.contentService.update(this.currentContent);
		updateCache();
		redirect();
		return null;
	}

	public List<Content> getContents() {
		return contents;
	}
	
	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}
	
	public Content getCurrentContent() {
		return currentContent;
	}
	
	public void setType(String type) {
		this.type = ContentType.valueOfString(type);
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setNick(String nick) {
		this.nick = nick;
	}
	
	public void setLink(boolean link) {
		this.link = link;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public void setWebsite(String website) {
		this.website = website;
	}
	
	public ContentType getType() {
		return type;
	}
}
